<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <div class="heard">
          <el-button type="primary" size="small" @click="dialogFormVisible = true">添加经纪</el-button>
          <div class="input">
            <el-input v-model="input" placeholder="请输入内容" class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="searchFn" />
            </el-input>
          </div>
        </div>
        <el-table
          :data="jingjiList"
          border
          style="width: 100%; margin: 30px 0"
        >
          <el-table-column
            prop="image"
            label="图片"
            width="180"
          >
            <template v-slot="{row}">
              <el-image :src="row.image" style="width:100px;height:100px" />
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称"
          />
          <el-table-column
            prop="email"
            label="邮箱"
          />
          <el-table-column
            prop="phone"
            label="电话"
          />
          <el-table-column
            prop="address"
            label="地址"
          />
          <el-table-column
            prop="desc"
            label="介绍"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            width="200"
            label="操作"
          >
            <template v-slot="{row}">
              <el-button type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delFn(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          align="center"
          :total="total"
          :current-page="page"
          :page-size="limit"
          layout="prev, pager, next, total"
          @current-change="handleCurrentChange"
        />
      </el-card>

      <el-dialog :title="form.id ?'编辑经纪' :'添加经纪'" :visible="dialogFormVisible" @close="closeFn">
        <el-form ref="elForm" :model="form" :rules="rules">
          <el-form-item label="名称" prop="name" label-width="80px">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email" label-width="80px">
            <el-input v-model="form.email" />
          </el-form-item>
          <el-form-item label="电话" prop="phone" label-width="80px">
            <el-input v-model="form.phone" />
          </el-form-item>
          <el-form-item label="地址" prop="address" label-width="80px">
            <el-input v-model="form.address" />
          </el-form-item>
          <el-form-item label="描述" prop="desc" label-width="80px">
            <el-input v-model="form.desc" type="textarea" :rows="2" />
          </el-form-item>
          <el-form-item label="封面" label-width="80px">
            <ImageUpload ref="imgUpload" :staff-photo.sync="form.image" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeFn">取 消</el-button>
          <el-button type="primary" @click="addOrUpdateFn(form.id)">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getJingjiList, getJingjiCount, addJingji, oneJingji, editJingji, delJingji } from '@/api/jingji'
export default {
  data() {
    return {
      input: '',
      jingjiList: [],
      dialogFormVisible: false,
      form: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      total: 0,
      limit: 10,
      page: 1,
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入正确邮箱格式', pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入正确电话号码', pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getJingjiCount()
    this.getJingjiList()
  },
  methods: {
    // 初始化数据
    handleCurrentChange(page) {
      this.page = page
      this.getJingjiList(this.limit, this.page)
      this.getJingjiCount()
    },
    // 获取经纪列表数据
    async getJingjiList(a, b, c) {
      this.jingjiList = await getJingjiList(a = this.limit, b = this.limit * (this.page - 1), c = this.input)
    },
    // 获取经纪数量
    async getJingjiCount(a) {
      this.total = await getJingjiCount(a = this.input)
    },
    // 添加报错或更新保存回调
    async addOrUpdateFn(id) {
      await this.$refs.elForm.validate()
      if (id) {
        await editJingji(this.form)
        this.$message.success('更改成功！')
      } else {
        await addJingji(this.form)
        this.$message.success('添加成功！')
      }
      this.getJingjiList()
      this.getJingjiCount()
      this.closeFn()
    },
    // 取消或X回调
    closeFn() {
      this.$refs.elForm.resetFields()
      this.$refs.imgUpload.removeFn()
      this.form = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      this.dialogFormVisible = false
    },
    // 编辑按钮回调
    async editFn(id) {
      this.dialogFormVisible = true
      const { name, email, phone, address, desc, image } = await oneJingji(id)
      this.form = {
        id,
        name,
        desc,
        image,
        phone,
        address,
        email
      }
      this.$refs.imgUpload.fileList.push({
        url: this.form.image
      })
    },
    // 删除回调
    delFn(row) {
      this.$confirm(`是否删除${row.name}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delJingji(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getJingjiList()
        this.getJingjiCount()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchFn() {
      this.input = this.input.trim()
      this.getJingjiList(this.limit, this.page, this.input)
      this.getJingjiCount(this.input)
    }
  }
}
</script>

<style scoped>
  .heard{
    padding-bottom: 30px;
    border-bottom: 1px solid #eee;
  }
  .el-card{
    padding: 10px;
  }
  .input{
    float:right;
    width: 60%;
  }

</style>
